const redis = require('ioredis');
const { createPool } = require('generic-pool');

const defaultMinPoolSize = 5;  // 最小连接数
const defaultMaxPoolSize = 20; // 最大连接数
const defaultIdleTimeoutInMillis = 30 * 1000; // 毫秒，空闲连接的最大存活时间

module.exports = function () {
  return createPool({
    name:'redis', 
    create: () => new redis({
      port: process.env['PORT_REDIS'],
      host: process.env['HOST_REDIS'],
      password: process.env['PASSWORD_REDIS'], // 设置 Redis 服务器密码，如果没有密码则无需设置此项
      defaultExpiry: 60 * 3,
    }),
    destroy: (client) => client.disconnect(), 
  }, {
    max: defaultMaxPoolSize, 
    min: defaultMinPoolSize, 
    idleTimeoutMillis: defaultIdleTimeoutInMillis, 
    priorityRange: 3, 
    log: false 
  });
};
